import pygame

from Dialog.TunnelTaskDialog import TunnelTask1BeginDialog, TunnelTaskFailDialog, TunnelTask1EndDialog
from Scene.Task.BaseTask import BaseTask, TaskStatus


class TakePrizesTask(BaseTask):
    def __init__(self, yu_er, prizes_group, font):

        self.score = 0
        self.yu_er = yu_er
        self.prizes_group = prizes_group

        self.task_dialog = TunnelTask1BeginDialog(font)
        self.win_dialog = TunnelTask1EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)

        self.sound = pygame.mixer.Sound("../Resources/sound/prizes.mp3")

        super(TakePrizesTask, self).__init__(self.task_dialog, self.win_dialog, self.fail_dialog)

    def do_self_task(self, key_down, key_list):
        # 碰撞范围大一点
        self.yu_er.rect = pygame.Rect(self.yu_er.pos_x + 5, self.yu_er.pos_y + 5, 20, 60)
        # 碰撞检测
        collide_list = pygame.sprite.spritecollide(self.yu_er, self.prizes_group, True)
        # 再把范围改回来
        self.yu_er.rect = pygame.Rect(self.yu_er.pos_x + 15, self.yu_er.pos_y + 60, 20, 10)
        if len(collide_list) > 0:
            self.score += 1
            self.sound.play()
        if self.score > 5:
            self.task_status = TaskStatus.win_dialog
